package cn.com.guage.dtm.verify.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.guage.dtm.common.annotation.Log;
import cn.com.guage.dtm.common.enums.BusinessType;
import cn.com.guage.dtm.verify.domain.VerifyAccountTaskManage;
import cn.com.guage.dtm.verify.domain.VerifyTaskManage;
import cn.com.guage.dtm.verify.service.IVerifyAccountTaskManageService;
import cn.com.guage.dtm.common.core.controller.BaseController;
import cn.com.guage.dtm.common.core.domain.AjaxResult;
import cn.com.guage.dtm.common.utils.poi.ExcelUtil;
import cn.com.guage.dtm.metadata.domain.MdSys;
import cn.com.guage.dtm.common.core.page.TableDataInfo;

/**
 * 账务检核作业管理Controller
 * 
 * @author yangdechao
 * @date 2021-06-24
 */
@Controller
@RequestMapping("/verify/account")
public class VerifyAccountTaskManageController extends BaseController
{
    private String prefix = "verify/account";

    @Autowired
    private IVerifyAccountTaskManageService verifyAccountTaskManageService;

    @RequiresPermissions("verify:account:view")
    @GetMapping()
    public String account()
    {
        return prefix + "/account";
    }

    /**
     * 查询账务检核作业管理列表
     */
    @RequiresPermissions("verify:account:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VerifyAccountTaskManage verifyAccountTaskManage)
    {
        startPage();
        List<VerifyAccountTaskManage> list = verifyAccountTaskManageService.selectVerifyAccountTaskManageList(verifyAccountTaskManage);
        return getDataTable(list);
    }

    /**
     * 导出账务检核作业管理列表
     */
    @RequiresPermissions("verify:account:export")
    @Log(title = "账务检核作业管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VerifyAccountTaskManage verifyAccountTaskManage)
    {
        List<VerifyAccountTaskManage> list = verifyAccountTaskManageService.selectVerifyAccountTaskManageList(verifyAccountTaskManage);
        ExcelUtil<VerifyAccountTaskManage> util = new ExcelUtil<VerifyAccountTaskManage>(VerifyAccountTaskManage.class);
        return util.exportExcel(list, "账务检核作业管理数据");
    }

    /**
     * 新增账务检核作业管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	long timeNew = System.currentTimeMillis() / 1000; // 10位数的时间戳
		String taskCode = getAccountTaskNo("CHECK_ACC", String.valueOf(timeNew));
		mmap.put("taskCode", taskCode);
		mmap.put("className", "AccoutnCheckTaskClass");
		mmap.put("packageName", "cn.com.guage.dtm.svc.hengfeng.dkbs.job.check");
        return prefix + "/add";
    }

    /**
     * 新增保存账务检核作业管理
     */
    @RequiresPermissions("verify:account:add")
    @Log(title = "账务检核作业管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VerifyAccountTaskManage verifyAccountTaskManage)
    {
        return toAjax(verifyAccountTaskManageService.insertVerifyAccountTaskManage(verifyAccountTaskManage));
    }

    /**
     * 修改账务检核作业管理
     */
    @GetMapping("/edit/{taskId}")
    public String edit(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        VerifyAccountTaskManage verifyAccountTaskManage = verifyAccountTaskManageService.selectVerifyAccountTaskManageById(taskId);
        mmap.put("verifyAccountTaskManage", verifyAccountTaskManage);
        return prefix + "/edit";
    }

    /**
     * 修改保存账务检核作业管理
     */
    @RequiresPermissions("verify:account:edit")
    @Log(title = "账务检核作业管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VerifyAccountTaskManage verifyAccountTaskManage)
    {
        return toAjax(verifyAccountTaskManageService.updateVerifyAccountTaskManage(verifyAccountTaskManage));
    }

    /**
     * 删除账务检核作业管理
     */
    @RequiresPermissions("verify:account:remove")
    @Log(title = "账务检核作业管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(verifyAccountTaskManageService.deleteVerifyAccountTaskManageByIds(ids));
    }
    
    
    
    @RequiresPermissions("verify:task:detail")
	@GetMapping("/detail/{taskId}")
	public String detail(@PathVariable("taskId") Long taskId, ModelMap mmap) {
    	VerifyAccountTaskManage  verifyAccountTaskManage  = verifyAccountTaskManageService.selectVerifyAccountTaskManageById(taskId);
		mmap.put("verifyAccountTaskManage", verifyAccountTaskManage);
		return prefix + "/account-task-detail";
	}

	/**
	 * 任务类型+五位编号（从1开始，不够前补0）
	 * 
	 * @param equipmentType 设备类型
	 * @param equipmentNo   最新设备编号
	 * @return
	 */
	public static String getAccountTaskNo(String equipmentType, String equipmentNo) {
		String newEquipmentNo = equipmentType + "00001";

		if (equipmentNo != null && !equipmentNo.isEmpty()) {
			int no = Integer.parseInt(equipmentNo);
			int newEquipment = ++no;
			newEquipmentNo = String.format(equipmentType + "%05d", newEquipment);
		}

		return newEquipmentNo;
	}
}
